package com.mutouren.modules.sso.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mutouren.common.entity.Result;
import com.mutouren.common.entity.ResultCode;
import com.mutouren.common.orm.spring.BaseServiceImpl;
import com.mutouren.common.utils.UUIDUtils;
import com.mutouren.modules.sso.dao.RegisterDao;
import com.mutouren.modules.sso.model.Register;
import com.mutouren.modules.sso.service.RegisterService;

@Service
public class RegisterServiceImpl extends BaseServiceImpl<Register> implements RegisterService {

	@Resource
	private RegisterDao registerDao;

	@Override
	public void afterPropertiesSet() throws Exception {
		this.baseDao = registerDao;
	}

	@Override
	public void resetAppSecret(int appId) {
		String secret = UUIDUtils.createRandomUUID();
		this.registerDao.setAppSecret(appId, secret);
	}

	@Override
	public Result register(Register register) {
		Result result = new Result();
		
		if (registerDao.getByAppAlias(register.getAppAlias()) != null) {
			result.setCode(ResultCode.FAIL.value);
			result.setMessage("应用别名已注册");
			return result;
		}
		
		register.setAppSecret(UUIDUtils.createRandomUUID());
		registerDao.insert(register);
		
		result.setCode(ResultCode.SUCCESS.value);
		return result;
	}

	@Override
	public int add(Register register) {
		throw new RuntimeException("not permit direct call the add()");
	}

	@Override
	public Register getByAppAlias(String appAlias) {
		return this.registerDao.getByAppAlias(appAlias);
	}	
}
